// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/type/tree.h>
#include <gtest/gtest.h>

#include <cantor/type/type.h>
#include <cantor/type/type_kind.h>
#include <cantor/type/registry.h>

int main(int argc, char *argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

namespace cantor {
    TEST(type, type_registry) {
        TypeRegistry registry;
        auto ptr_rs = TUPLE({INT64(), STRING(), INT8()});

        ASSERT_TRUE(registry.register_type("ns.db.tuple1", std::move(ptr_rs).value_or_die()).ok());

        auto ptr_rs1 = TUPLE({INT64(), STRING(), INT8()});

        ASSERT_TRUE(registry.register_type("ns.tuple2", std::move(ptr_rs1).value_or_die()).ok());

        auto ptr_rs2 = TUPLE({INT64(), STRING(), INT8()});

        ASSERT_TRUE(registry.register_type("tuple2", std::move(ptr_rs2).value_or_die()).ok());

        auto ptr1 = STRUCT({"a", "b", "c"}, {INT64(), STRING(), INT32()}).value_or_die();
        ASSERT_TRUE(registry.register_type("ns.db.struct1", ptr1).ok());

        auto rs = registry.resolve_type("ns.db.tuple1");
        ASSERT_TRUE(rs.ok());
        ASSERT_NE(rs.value_or_die(), nullptr);

        auto rs1 = registry.resolve_type("ns.db.tuple2");
        ASSERT_TRUE(rs1.ok())<<rs1.status().to_string();
        auto tuple1 = rs1.value_or_die();
        ASSERT_NE(rs1.value_or_die(), nullptr);
        auto rs2 = registry.resolve_type("ns.db.table.tuple2");
        ASSERT_TRUE(rs1.ok())<<rs1.status().to_string();
        ASSERT_NE(rs1.value_or_die(), nullptr);

        auto udf_rs = UDF("ns.db.tuple2", tuple1);
        ASSERT_TRUE(udf_rs.ok());
        auto udf = std::move(udf_rs).value_or_die();
        ASSERT_NE(udf, nullptr);
        ASSERT_TRUE(udf->has_real_type());
        ASSERT_NE(udf->type_ptr(), nullptr);
    }
}